package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentMemberCompanyWebBinding;
import com.sgb.member.entity.MemberBean;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import java.util.ArrayList;

public class CompanyWebFragment extends AppLazyFragment {

    private FragmentMemberCompanyWebBinding mBinding;
    private ArrayList<Fragment> fragments;
    private int curPos;

    /**
     *
     * MemberBean bean = new MemberBean();
     * bean.setMemberUpdateLeverType(); // //1供方会员 2需方会员
     * bean.setCompanyNo();
     */
    public static CompanyWebFragment newInstance(int position, MemberBean bean) {
        CompanyWebFragment fragment = new CompanyWebFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("INDEX", position);
        bundle.putSerializable("COMPANY_NO", bean);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_member_company_web, container, false);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        assert getArguments() != null;
        curPos = getArguments().getInt("INDEX");
        MemberBean company = (MemberBean) getArguments().getSerializable("COMPANY_NO");
        fragments = new ArrayList<>();
        String[] mTabs = null;
        switch (curPos) {
            case 0:
                // 调整经营
                mTabs = new String[]{"经营商品", "租赁经营", "工程工队"};
                fragments.add(CompanyWebBusinessFragment.newInstance(0, company));
                fragments.add(CompanyWebBusinessFragment.newInstance(1, company));
                fragments.add(CompanyWebBusinessFragment.newInstance(2, company));
                break;
            case 1:
                // 会员招采
                break;
            case 2:
                // 会员集采
                break;
            case 3:
                // 需求公告
                mTabs = new String[]{"商品询价", "求租需求"};
                fragments.add(CompanyWebDemandFragment.newInstance(0, company));
                fragments.add(CompanyWebDemandFragment.newInstance(1, company));
                break;
            case 4:
                // 企业简介
                break;
        }
        if (fragments.size() == 0 || mTabs == null) {
            return;
        }
        mBinding.tabLayout.setVisibility(View.VISIBLE);
        SlidingFragmentPagerAdapter adapter = new SlidingFragmentPagerAdapter(getChildFragmentManager(), fragments, mTabs);
        mBinding.viewPager.setAdapter(adapter);
        mBinding.viewPager.setOffscreenPageLimit(mTabs.length);
        mBinding.tabLayout.setupWithViewPager(mBinding.viewPager);
        mBinding.tabLayout.addOnTabSelectedListener(mTabs, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mBinding.viewPager.setCurrentItem(tab.getPosition());
            }
        });
    }

    @Override
    protected void onLazyLoad() {
        if (curPos == 3 && fragments.size() > 0) {
            int currentItem = mBinding.viewPager.getCurrentItem();
            CompanyWebDemandFragment fragment = (CompanyWebDemandFragment) fragments.get(currentItem);
            fragment.onLazyLoad();
        }
    }
}